package com.ibm.productivity.tools.blogplugin.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * the TitleInputDialog is used to get the title of 
 * a new post article.
 * Use open() to show the dialog
 * Use getTitle() to get the title String
 * 
 * @author TangQiao 2008-6-11
 * 
 */
public class TitleInputDialog extends Dialog {

	String title;
	int selectedIndex;
	String [] blogNames;
	
	public void setBlogNames(String [] blogNames) {
		this.blogNames = blogNames;
	}
	
	public int getSelectedIndex() {
		return selectedIndex;
	}

	public String getTitle(){
		title = title.trim();
		if (title.length() == 0) return null;
		return title;
	}
	
	/**
	 * @param parent
	 */
	public TitleInputDialog(Shell parent) {
		super(parent);
	}

	/**
	 * @param parent
	 * @param style
	 * 
	 */
	public TitleInputDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Makes the dialog visible.
	 * 
	 * @return
	 */
	public void open() {
		Shell parent = getParent();
		final Shell shell = new Shell(parent, SWT.TITLE | SWT.BORDER
				| SWT.APPLICATION_MODAL);
		shell.setText("Title Input Dialog");
		
		GridData gridLayout = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_END);
		gridLayout.heightHint = 80;
		gridLayout.widthHint = 120;

		shell.setLayout(new GridLayout(2, true));

		Label label = new Label(shell, SWT.NULL);
		label.setText("Input the article title:");
		label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
		text.setLayoutData(gridLayout);
		
		Label label2 = new Label(shell, SWT.NULL);
		label2.setText("publish on:");
		label2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		
		final Combo combo = new Combo(shell, SWT.DROP_DOWN);
		combo.setItems(blogNames);
		combo.select(0);
		combo.setLayoutData(gridLayout);
		
		final Button buttonOK = new Button(shell, SWT.PUSH);
		buttonOK.setText("  Ok  ");
		buttonOK.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		Button buttonCancel = new Button(shell, SWT.PUSH);
		buttonCancel.setText("Cancel");

		text.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				try {
					title = text.getText();
								
					if (title.length()!=0)
						buttonOK.setEnabled(true);
					else
						buttonOK.setEnabled(false);
				} catch (Exception e) {
					buttonOK.setEnabled(false);
				}
			}
		});
		
		

		// button OK pushed 
		buttonOK.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				selectedIndex = combo.getSelectionIndex();
				shell.dispose();
			}
		});

		// button Cancel pushed
		buttonCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				title = null;
				shell.dispose();
			}
		});

		shell.addListener(SWT.Traverse, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.TRAVERSE_ESCAPE)
					event.doit = false;
			}
		});

		text.setText("");
		
		title = null;
		shell.pack();
		shell.open();

		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

	}

	public static void main(String[] args) {
		Shell shell = new Shell();
		TitleInputDialog dialog = new TitleInputDialog(shell);
		dialog.setBlogNames(new String[]{"My first blog", "my second blog"});
		dialog.open();
		System.out.println(dialog.getTitle());
		System.out.println(dialog.getSelectedIndex());
		
	}
}
